package com.entrogy.controller;

import com.entrogy.bean.MenuEntity;
import com.entrogy.bean.Msg;
import com.entrogy.bean.Role;
import com.entrogy.bean.UserEntity;
import com.entrogy.service.MenuService;
import com.entrogy.utils.ApiReturnUtil;
import com.entrogy.utils.constants.ErrorEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(description = "登录成功用户，PC界面菜单渲染接口")
@RestController
@RequestMapping(value = "/menucontroller")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    private Map<String,Object> map = new HashMap<String, Object>();

    @Autowired
    private MenuService menuService;

    /**
     * 根据传入的userId将其所对应的菜单项查询出来
     * @param
     * @return
     */
//    @RequiresRoles({"校长"})
    @ApiOperation(value = "通过用户id来获取菜单项获取权限")
    @RequestMapping(value = "/userMenu",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Integer", paramType = "query")
            //@ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String", paramType = "query")
    })
    public Msg userMenu() {
        UserEntity currentUser = (UserEntity) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        String userId = currentUser.getId();
        int schoolCode = currentUser.getSchoolCode();
        List<MenuEntity> menus = new ArrayList<>();
        menus = menuService.getMenuByUserId(userId, schoolCode);
        if (!menus.isEmpty()){
            return Msg.success().add("menus", menus);
        }
        return Msg.fail(ErrorEnum.E_101,ErrorEnum.E_100.getErrorMsg());
    }

    @ApiOperation(value = "通过角色来获取菜单项获取权限")
    @RequestMapping(value = "/roleMenu",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = false, dataType = "Integer", paramType = "query")
    })
    public Object roleMenu(Integer roleId) {
        String roleName = new String();
        UserEntity currentUser=null;
        if(roleId == null){
             currentUser = (UserEntity) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
             if (currentUser == null){
                 return "当前用户未登录！";
             }
            Set<Role> roles = currentUser.getRoles();
            //如果没有选择角色，那么将会默认集合中的第一个
            for (Role role : roles){
                roleId = Math.toIntExact(role.getrId());
                roleName = role.getRoleName();
            }
        }
        List<MenuEntity> menus = menuService.findMenusByRoleId(roleId);
        if (!menus.isEmpty()){
            Map<String,Object> map = new HashMap<>();
            map.put("roleName",roleName);
            map.put("userName",currentUser.getName());
            map.put("menus", menus);
            return map;
        }
        return Msg.fail(ErrorEnum.E_101, ErrorEnum.E_100.getErrorMsg());
    }
}
